<!-- 表单弹窗 -->
<form id="pointsRule-form" lay-filter="pointsRule-form" class="layui-form model-form">
    <input name="poruid" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">编号</label>
        <div class="layui-input-block">
            <input name="prcode" placeholder="请输入规则编号" type="text" class="layui-input"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">类型</label>
        <div class="layui-input-block">
            <select name="prtype" lay-verify="required" required>
                <option value="">请选择规则类型</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">描述</label>
        <div class="layui-input-block">
            <input name="porule" placeholder="请输入规则描述" type="text" class="layui-input"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">积分</label>
        <div class="layui-input-block">
            <input name="points" placeholder="请输入积分值" type="text" class="layui-input"
                   lay-verify="number" required/>
        </div>
    </div>
    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" ew-event="closePopup" type="button">取消</button>
        <button class="layui-btn" lay-filter="pointsRule-form-submit" lay-submit>保存</button>
    </div>
</form>

<script>
    layui.use(['layer', 'admin', 'form', 'laydate', 'config'], function () {
        let layer = layui.layer;
        let admin = layui.admin;
        let form = layui.form;
        let laydate = layui.laydate;
        let config = layui.config;

        //开启公历节日
        laydate.render({
            elem: '#endtim'
            , calendar: true
            , trigger: 'click'
            , type: 'datetime'
        });

        // 填充表单数据
        let pointsRuleType = admin.getTempData('t_pointsRuleType');
        for (let item of pointsRuleType) {
            let option = new Option(item.prtyne, item.prtycd);
            $("#pointsRule-form select[name='prtype']").append(option);
        }
        let points = admin.getTempData('t_pointsRule');
        if (points) {
            $("#pointsRule-form input[name='prcode']").attr('disabled',true);
            form.val('pointsRule-form', points);
        }
        form.render();

        // 表单提交事件
        form.on('submit(pointsRule-form-submit)', function (data) {
            layer.load(2);
            console.log(JSON.stringify(data.field));
            admin.req('api-points/pointsRule/saveOrUpdate', JSON.stringify(data.field), function (data) {
                layer.closeAll('loading');
                if (data.code === 0) {
                    layer.msg(data.msg, {icon: 1, time: 500});
                    admin.finishPopupCenter();
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                }
            }, 'POST');
            return false;
        });
    });
</script>
